import axios from 'axios';
import RequestError from '../base/exceptions/RequestError';

const postProcess = response => response.data;

const request = async (url, options) => {
    const headers = {
        Accept: 'application/vnd.github.mercy-preview+json'
    };
    return await axios({url, timeout: 8000, headers, ...options })
        .then(postProcess)
        .catch (error => {
            const response = error.response;
            if(response && response.data.message === 'Validation Failed') {
                return new RequestError('Validation Failed', error.response.data.errors)
            } else {
                return new RequestError(error.response.data.message, error.response.data.errors);
            }
        });
}

export default request;